package com.sgb.goods.widget.pop;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.R;
import com.sgb.goods.entity.GoodsBrandEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.widget.pop.adapter.GoodsBrandAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

/**
 * 描述：商品品牌选择弹窗
 *
 * @author qiangHeNooo
 * <p>
 * 时间：2021.12.09
 */
public class GoodsBrandPopWindow extends BasePopWindow<GoodsBrandEntity> {

    private RecyclerView recyclerView;
    private GoodsBrandAdapter brandAdapter;
    private LinearLayout noDataLay;
    private String brandId = "";
    private String brandName = "";
    private LinearLayout linearLayout;
    private EditText editText;
    private InputTextInterface inputTextInterface;
    /**
     * 商品分类ID
     */
    private String cid3 = "";
    private final boolean isShowSearch;
    private final String CODE_NAME = "3_pinpaiguanli";

    public GoodsBrandPopWindow(Context context, boolean isShowSearch) {
        super(context, DIR_DOWN_UP);
        this.isShowSearch = isShowSearch;
    }

    @Override
    protected int popLayout() {
        return R.layout.goods_layout_pop_window_brand;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.skill_list);
        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        editText = holder.getPop().findViewById(R.id.et_view);
        noDataLay = holder.getPop().findViewById(R.id.ll_empty);
        TextView resetView = holder.getPop().findViewById(R.id.reset_view);
        linearLayout = holder.getPop().findViewById(R.id.ll_add);
        okView.setOnClickListener(v -> {
            dissMiss();
            if (onResultClick != null) {
                onResultClick.result(brandId, brandName, null);
            }
        });
        resetView.setOnClickListener(v -> dismiss());
        linearLayout.setOnClickListener(view -> {
            if (SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.IS_DATA, "").contains(CODE_NAME)) {
                ARouter.getInstance().build(Constants.ARouter.AR_ADD_GOODS_BRAND).withString("cid3", cid3).navigation();
            } else {
                showNoAuthorityDialog();
            }
        });
        editText.setOnEditorActionListener((textView, actionId, keyEvent) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                inputTextInterface.input(editText.getText().toString().trim());
                return true;
            }
            return false;
        });
        brandAdapter = new GoodsBrandAdapter(mContext, null);
        brandAdapter.setItemSelect(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(brandAdapter);
        brandAdapter.iSelectChild((name, id) -> {
            brandName = name;
            brandId = id;
        });
    }

    private void showNoAuthorityDialog() {
        View view = View.inflate(mContext, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(mContext, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);
        TextView tvConfirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tvMessage = view.findViewById(R.id.tv_message);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setVisibility(View.VISIBLE);
        TextView tvLogoutCancel = view.findViewById(R.id.tv_logout_cancel);
        tvTitle.setText("温馨提示");
        tvConfirm.setText("确定");
        tvMessage.setText("该账户无权限，请联系企业管理员");
        tvConfirm.setOnClickListener(v -> roundCornerDialog.dismiss());
        tvLogoutCancel.setOnClickListener(v -> roundCornerDialog.dismiss());
    }

    public void setData(List<GoodsBrandEntity> data, String cid3, boolean isUpdate) {
        this.cid3 = cid3;
        brandAdapter.setDatas(data);
        if (isUpdate) {
            brandAdapter.setDefItem(-1);
        }
        brandAdapter.isUpdate(isUpdate);
        if (!isHaveBrand(data)) {
            noDataLay.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            noDataLay.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
        if (!isShowSearch) {
            linearLayout.setVisibility(View.GONE);
            editText.setVisibility(View.GONE);
        }
    }

    private boolean isHaveBrand(List<GoodsBrandEntity> data) {
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getChildList() != null && data.get(i).getChildList().size() > 0) {
                return true;
            }
        }
        return false;
    }

    public interface InputTextInterface {
        /**
         * 用于回调的接口
         *
         * @param s 输入的字符串
         */
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }

}
